/* 
 * Copyright 2010 Ziya Guven Koylu, Ozge Koylu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.koylu.caffein.model.clazz.validation;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

import com.koylu.caffein.model.caffein.CaffeinConfig;
import com.koylu.caffein.model.clazz.Clazz;
import com.koylu.caffein.model.clazz.Field;
import com.koylu.caffein.validator.Rule;
import com.koylu.caffein.validator.ValidatorManager;

public class Validation {
	
	private String name;
	private String validateFor = ValidateFor.ALL.name();
	private String min;
	private String max;
	private String mask;
	private String message;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getValidateFor() {
		return validateFor;
	}
	
	public void setValidateFor(String validateFor) {
		this.validateFor = validateFor.toUpperCase(Locale.ENGLISH);
	}
	
	public String getMin() {
		return min;
	}
	
	public void setMin(String min) {
		this.min = min;
	}
	
	public String getMax() {
		return max;
	}
	
	public void setMax(String max) {
		this.max = max;
	}
	
	public String getMessage() {
		return message;
	}
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getMask() {
		return mask;
	}

	public void setMask(String mask) {
		this.mask = mask;
	}
	
	public void validate(CaffeinConfig caffeinConfig, Clazz clazz, Field field, Object object, ValidateFor _validateFor) throws Exception {
		ValidateFor vf = ValidateFor.valueOf(validateFor);
		if( ValidateFor.ALL.equals(vf) || vf.equals(_validateFor) ){
			Rule rule = ValidatorManager.findRule(getName());
			if(rule != null){
				Map<String,Object> parameterMap = new HashMap<String,Object>(11);
				//caffein clazz field parameters
				parameterMap.put("caffeinConfig", caffeinConfig);
				parameterMap.put("object", object);
				parameterMap.put("clazz", clazz);
				parameterMap.put("field", field);
				parameterMap.put("fieldName", field.getName());
				parameterMap.put("fieldValue", PropertyUtils.getNestedProperty(object, field.getPath(caffeinConfig)));
				parameterMap.put("validateFor", _validateFor.name());
				//field validation parameters
				if(min != null){
					try {
						parameterMap.put("min", Integer.parseInt(min));						
					} catch (Exception ignored) {}
				}
				if(max != null){
					try {
						parameterMap.put("max", Integer.parseInt(max));						
					} catch (Exception ignored) {}
				}
				if(mask != null){
					parameterMap.put("mask", mask);
				}
				if(message != null){
					parameterMap.put("message", message);
				}
				if(!rule.validate(parameterMap)){
					throw new ValidationException(rule, parameterMap);
				}
			}else{
				throw new ValidationRuleNotFoundException(field, name, "Validation rule not found");
			}	
		}
	}
}